package cash_access;

import mware_lib.Config;
import mware_lib.NameServiceMessage;
import mware_lib.Stub;


public class TransactionStub extends TransactionImplBase {
    private NameServiceMessage msg;
    private String splitString = Config.splitString;

    public TransactionStub(NameServiceMessage msg) {
        this.msg = msg;
    }

    @Override
    public void deposit(String accountId, double amount) throws InvalidParamException {
        if(Config.DEBUGGER) System.out.println("Deposit mit accountId: " + accountId + ", amount: "+ amount +" und MSG :" +msg.toString());
        Stub stub = new Stub();
        stub.setReference(msg);
        stub.setMethod("deposit");
        stub.addParameter(String.class, accountId);
        stub.addParameter(double.class, amount);

        String receive = stub.send();
        String[] splits = receive.split(splitString);
        if (splits[0].equals("ERROR")){
            if(Config.DEBUGGER) System.out.println("deposit ERROR");
            if(Config.DEBUGGER) System.out.println("Transaction InvalidParamException : " + splits[2] );
            throw new InvalidParamException(splits[2]);
        }
    }

    @Override
    public void withdraw(String accountId, double amount) throws InvalidParamException, OverdraftException {
        if(Config.DEBUGGER) System.out.println("withdraw mit accountId: " + accountId + ", amount: "+ amount +" und MSG :" +msg.toString());
        Stub stub = new Stub();
        stub.setReference(msg);
        stub.setMethod("withdraw");
        stub.addParameter(String.class, accountId);
        stub.addParameter(double.class, amount);

        String receive = stub.send();
        String[] splits = receive.split(splitString);
        if (splits[0].equals("ERROR")) {
            if(Config.DEBUGGER) System.out.println("withdraw ERROR " + splits[1]);
            if (splits[1].contains("OverdraftException")) {
                if(Config.DEBUGGER) System.out.println("Transaction OverdraftException : " + splits[2] );
                throw new OverdraftException(splits[2]);
            } else if (splits[1].contains("InvalidParamException")) {
                if(Config.DEBUGGER) System.out.println("Transaction InvalidParamException : " + splits[2] );
                throw new InvalidParamException(splits[2]);
            }
        }
    }

    @Override
    public double getBalance(String accountId) throws InvalidParamException {
        if(Config.DEBUGGER) System.out.println("getBalance mit accountId: " + accountId + " und MSG :" +msg.toString());
        Stub stub = new Stub();
        stub.setReference(msg);
        stub.setMethod("getBalance");
        stub.addParameter(String.class, accountId);

        String receive = stub.send();
        String[] splits = receive.split(splitString);
        if (splits[0].equals("ERROR")) {
            if(Config.DEBUGGER) System.out.println("getBalance ERROR");
            if(Config.DEBUGGER) System.out.println("Transaction InvalidParamException : " + splits[2] );
            throw new InvalidParamException(splits[2]);
        }
        return Double.parseDouble(receive);
    }
}
